//
//  MLDiscoverViewController.m
//  MLBleSDK
//
//  Created by 程杰 on 2023/10/18.
//  $(SRCROOT)/MLBleSDK/Main/PrefixHeader.pch /Users/chengjie/Documents/ML-Sdk/gitee-private/ml-sdk-source/ble/BleV2.0/MLBleSDK/MLBleSDK/Main/PrefixHeader.pch

#import "MLDiscoverViewController.h"


#import "BleInfoViewController.h"

@interface MLDiscoverViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSArray<MLBLEModel *> *devices;
@property (nonatomic, strong) UIRefreshControl *refreshControl;

@end

@implementation MLDiscoverViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.title = @"蓝牙发现";
    self.view.backgroundColor = [UIColor whiteColor];
    self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.tableFooterView = [UIView new];
    [self.view addSubview:self.tableView];
    
    
    
    // 创建 UIRefreshControl
    self.refreshControl = [[UIRefreshControl alloc] init];
    // 设置刷新时的触发事件
    [self.refreshControl addTarget:self action:@selector(refreshData) forControlEvents:UIControlEventValueChanged];
    // 将 UIRefreshControl 添加到 UITableView 或 UICollectionView
    [self.tableView addSubview:self.refreshControl];
    [self refreshData];
    
    [[MLBLEManager sharedManager] mlBleScan];

}
- (void)refreshData {

    
    
    [[MLBLEManager sharedManager] mlBleScan];
    self.devices = nil;
    [[MLBLEManager sharedManager] scanPeripheralsWithFilter:YES completion:^(NSArray<MLBLEModel *> * _Nonnull modes) {
    
        self.devices = modes;
        [self.tableView reloadData];
        
        // 刷新完成后，结束刷新动画
        [self.refreshControl endRefreshing];
    }];
}
- (void)viewWillAppear:(BOOL)animated {
    
    [self refreshData];
}
- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [SVProgressHUD dismiss];
}
#pragma mark - UITableViewDataSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.devices.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *cellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];
    }
    
    MLBLEModel *device = self.devices[indexPath.row];
    cell.textLabel.text = device.name;
    NSString *mac = @"uuid:";
    if(device.mac) {
        mac = device.mac;
    }
    if (device.deviceIdentifier) {
        NSString *det = [[mac stringByAppendingString:@"<-->"] stringByAppendingString:device.deviceIdentifier];
        cell.detailTextLabel.text = det;
    }
    
    return cell;
}

#pragma mark - UITableViewDelegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    MLBLEModel *model = self.devices[indexPath.row];
//    [[MLBLEManager sharedManager] connectToPeripheral:model.peripheral]
    [SVProgressHUD showWithStatus:@"开始连接"];
    [[MLBLEManager sharedManager] connectToPeripheral:model.peripheral callback:^(BOOL success, CBPeripheral * _Nonnull peripheral, NSError * _Nonnull error) {
        if (success) {
            [[MLBLEManager sharedManager] setNotifyisSuccess:^(BOOL isSuccess) {
                if (success) {
                    
                    /// 开启心跳，可不用开启； 默认10秒发送一个心跳
//                    [[MLBLEManager sharedManager] startHeartbeatTimerForPeripheral:peripheral];
                    
                    [[MLBLEManager sharedManager] mlBleStopScan];
                    [SVProgressHUD showSuccessWithStatus:@"连接成功"];
                    BleInfoViewController *vc = [BleInfoViewController new];
                    vc.model = model;
                    [self.navigationController pushViewController:vc animated:YES];
                }
            }];
            
            
            
        }
      
    }];
    

}


@end
